using System;
using Anvil.Core;

namespace Anvil.Core.Commands
{
	/// <summary>
	/// Invoke a query that returns the count of a result set, 
	/// adding it to any previous count.
	/// </summary>
	public class BaseTally : BaseList
	{
		private object Add(object s1, object s2)
		{
			int one = 0;
			int two = 0;

			if (null != s1) one = Convert.ToInt32(s1);
			if (null != s2) two = Convert.ToInt32(s2);

			int total = one + two;

			return Convert.ToString(total);
		}


		public override bool RequestExecute(IRequestContext context)
		{
			PreProcess(context);
			object tally = context[Tokens.ITEM_COUNT];
			object result = Mapper.QueryForObject(QueryID, context);
			object total = Add(tally, result);
			context[Tokens.ITEM_COUNT] = total;
			return CONTINUE;
		}
	}
}